package com.hntx.joymusic.sytRetrofity;

import com.google.gson.JsonObject;
import com.hntx.joymusic.util.GsonUtils;
import com.hntx.joymusic.yun.BatchGetFileDownloadUrl;
import com.hntx.joymusic.yun.CoShareFile;
import com.hntx.joymusic.yun.CommitCorpFile;
import com.hntx.joymusic.yun.CopyFileToFile;
import com.hntx.joymusic.yun.CorpUploadFile;
import com.hntx.joymusic.yun.FileDownloadUrl;
import com.hntx.joymusic.yun.JoinCorpCoshare;
import com.hntx.joymusic.yun.PiFiles;
import com.hntx.joymusic.yun.ShareFolders;

import java.util.Map;

import io.reactivex.Observable;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class SytRetrofit extends SytRetrofitBase {
    private static final String TAG = "SytRetrofit";
    private SytApi retrofits = getRetrofit().create(SytApi.class);
    private SytApi retrofitsJQ = getRetrofitJQ().create(SytApi.class);
    private SytApi retrofitNewJQ = getRetrofitNewJQ().create(SytApi.class);
    private static SytRetrofit mInstance;

    public static SytRetrofit getInstance() {
        if (mInstance == null)
            try {
                synchronized (SytRetrofit.class) {
                    if (mInstance == null)
                        mInstance = new SytRetrofit();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        return mInstance;
    }

//    public Observable<JsonObject> postContentAuth(String baseUrl, String token, ContentAuthBean contentAuthBean) {
//        try {
//            Log.d("VideoPlay:", "正在请求鉴权" + token);
//            JSONObject json = new JSONObject();
//            try {
//                json.put("cid", contentAuthBean.getCid());
//                json.put("tid", contentAuthBean.getTid());
//                json.put("supercid", "");
//                json.put("playType", contentAuthBean.getPlayType());
//                json.put("contentType", contentAuthBean.getContentType());
//                json.put("businessType", contentAuthBean.getBusinessType());
//                json.put("idflag", "1");
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
//            RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(json));
//            return getRetrofit(baseUrl, token).create(SytApi.class).PostContentAuth(body);
//        } catch (Exception e) {
//            Log.d("VideoPlay:", "鉴权请求：" + e.getMessage());
//            e.printStackTrace();
//        }
//        return null;
//    }

    public Observable<JsonObject> postCollect(String songId, String userid, String platform, String collect) {
        try {
            //"collect"
            return retrofits.postCollect(collect, songId, userid, platform);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
//
    public Observable<JsonObject> postDel(String songId, String userid, String platform) {
        try {
            return retrofits.postDel("delPlayerChecked", songId, userid, platform, "HD");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
//
//    public Observable<Integer> getNewVersionCode() {
//        try {
//            return retrofits.getNewVersionCode();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    public Observable<ResponseBody> downLoadApk(String path) {
//        try {
//            return retrofits.download(path);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    public Observable<String> upLoadFile(String path) {
//        File file = new File(path);
//        if (!file.exists()) return null;
//        RequestBody requestFile = RequestBody.create(MediaType.parse("audio/mp3"), file);
//        //注意这里的file是对应MultipartBody上传文件的key名称
//        MultipartBody.Part multipartBody = MultipartBody
//                .Part.createFormData("file", file.getName(), requestFile);
//        return retrofits.upFile(multipartBody);
//    }

    public Observable<JsonObject> getYunToken(String appKey, String appSignature, String grantType, String account, Long timestamp) {

        try {
            return getYunRetrofit().create(SytApi.class).getYunToken(appKey, appSignature, grantType, account, timestamp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<JsonObject> queryYunList(String accessToken, String signature, String date, String appkey) {
        try {
            return getYunRetrofit().create(SytApi.class).queryYunList(accessToken, signature, date, appkey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> getCorpInfo(String corpId, String accessToken, String signature, String date) {
        try {
            return getYunRetrofit().create(SytApi.class).getCorpInfo(corpId, accessToken, signature, date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> getCorpMemberList(String corpId, String accessToken, String signature, String date) {
        try {
            return getYunRetrofit().create(SytApi.class).getCorpMemberList(corpId, accessToken, signature, date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> getSpecifiedFiles(Map<String, Object> map, String accessToken, String signature, String date) {
        try {
            return getYunRetrofit().create(SytApi.class).getSpecifiedFiles(accessToken, signature, date, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<JsonObject> createCorpFiles(String accessToken, String signature, String date, String appKey, String corpId, String parentId, String fileName) {
        try {
            return getYunRetrofit().create(SytApi.class).createCorpFiles(accessToken, signature, date, appKey, corpId, parentId, fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<JsonObject> getCorpPutUploadURL(String accessToken, String signature, String date, String appKey, String corpId) {
        try {
            return getYunRetrofit().create(SytApi.class).getCorpPutUploadURL(accessToken, signature, date, appKey, corpId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> piCreateFiles(String accessToken, String signature, String date, PiFiles piFiles) {
        try {
            RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"), GsonUtils.getGson().toJson(piFiles));
            return getYunRetrofit().create(SytApi.class).piCreateFiles(accessToken, signature, date, body);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> cxSingleCorpInfo(String accessToken, String signature, String date, String corpId, String fileId) {
        try {
            return getYunRetrofit().create(SytApi.class).cxSingleCorpInfo(accessToken, signature, date, corpId, fileId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> cxSingleCorpFileInfo(String accessToken, String signature, String date, String corpId, String fileId) {
        try {
            return getYunRetrofit().create(SytApi.class).cxSingleCorpFileInfo(accessToken, signature, date, corpId, fileId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> deleteFile(String accessToken, String signature, String date, String corpId, String fileIdList) {

        try {
            return getYunRetrofit().create(SytApi.class).deleteFile(accessToken, signature, date, corpId, fileIdList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> renameFile(String accessToken, String signature, String date, String corpId, String fileId, String fileName) {
        try {
            return getYunRetrofit().create(SytApi.class).renameFile(accessToken, signature, date, corpId, fileId, fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> copyFile(String accessToken, String signature, String date, CopyFileToFile fileToFile) {
        try {
            RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"), GsonUtils.getGson().toJson(fileToFile));
            return getYunRetrofit().create(SytApi.class).copyFile(accessToken, signature, date, body);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> createCoshare(String accessToken, String signature, String date, String corpId, String coshareName) {
        try {
            return getYunRetrofit().create(SytApi.class).createCoshare(accessToken, signature, date, corpId, coshareName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> listJoinCorpCoshare(String accessToken, String signature, String date, JoinCorpCoshare corpCoshare) {
        try {
            RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"), GsonUtils.getGson().toJson(corpCoshare));
            return getYunRetrofit().create(SytApi.class).listJoinCorpCoshare(accessToken, signature, date, body);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> listCoshareFile(String accessToken, String signature, String date, CoShareFile corpCoshare) {
        try {
            RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"), GsonUtils.getGson().toJson(corpCoshare));
            return getYunRetrofit().create(SytApi.class).listCoshareFile(accessToken, signature, date, body);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> createCoshareFolders(String accessToken, String signature, String date, ShareFolders shareFolders) {
        try {
            RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"), GsonUtils.getGson().toJson(shareFolders));
            return getYunRetrofit().create(SytApi.class).createCoshareFolders(accessToken, signature, date, body);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> getCoshareFileInfo(String accessToken, String signature, String date, String corpId, String coshareId) {
        try {
            return getYunRetrofit().create(SytApi.class).getCoshareFileInfo(accessToken, signature, date, corpId, coshareId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> getsingleCoshareFileInfo(String accessToken, String signature, String date, String corpId, String coshareId, String fileId) {
        try {
            return getYunRetrofit().create(SytApi.class).getsingleCoshareFileInfo(accessToken, signature, date, corpId, coshareId, fileId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> renamecoshare(String accessToken, String signature, String date, String corpId, String coshareId, String coshareName) {
        try {
            return getYunRetrofit().create(SytApi.class).renamecoshare(accessToken, signature, date, corpId, coshareId, coshareName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> createCoshareFile(String accessToken, String signature, String date, String corpId, String coshareId,
                                                String parentId, String fileName) {
        try {
            return getYunRetrofit().create(SytApi.class)
                    .createCoshareFile(accessToken, signature, date, corpId, coshareId, parentId, fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> renameCoshareFile(String accessToken, String signature, String date,
                                                String corpId, String coshareId, String fileId, String newFileName) {
        try {
            return getYunRetrofit().create(SytApi.class)
                    .renameCoshareFile(accessToken, signature, date, corpId, coshareId, fileId, newFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> putShareFile2Recycle(String accessToken, String signature,
                                                   String date, String corpId, String coshareId,
                                                   String fileIdList) {
        try {
            return getYunRetrofit().create(SytApi.class)
                    .putShareFile2Recycle(accessToken, signature, date, corpId, coshareId, fileIdList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<JsonObject> putCorpFile(String url,byte[] bytes,Map<String, String> map) {
        try {
            RequestBody requestBody = RequestBody.create(MediaType.parse("audio/mp3"), bytes);
            return getYunUpRetrofit().create(SytApi.class)
                    .putCorpFile(url,requestBody,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> createCorpUploadFile(Map<String, String> headerMap,
                                                   CorpUploadFile uploadFile) {
        try {
            RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"), GsonUtils.getGson().toJson(uploadFile));
            return getYunRetrofit().create(SytApi.class).createCorpUploadFile(headerMap, body);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> getCorpUploadFileStatus(Map<String, String> headerMap, Long uploadFileId, Long corpId, String isLog) {
        try {
            return getYunRetrofit().create(SytApi.class)
                    .getCorpUploadFileStatus(headerMap, uploadFileId, corpId, isLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> uploadCorpFile(Map<String, String> map) {
        try {
            return getYunRetrofit().create(SytApi.class)
                    .uploadCorpFile(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> commitCorpFile(Map<String, String> headerMap, CommitCorpFile corpFile) {
        try {
            RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"), GsonUtils.getGson().toJson(corpFile));
            return getYunRetrofit().create(SytApi.class).commitCorpFile(headerMap, body);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> getFileDownloadUrl(Map<String, String> headerMap, FileDownloadUrl fileDownloadUrl) {
        try {
            RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"), GsonUtils.getGson().toJson(fileDownloadUrl));
            return getYunRetrofit().create(SytApi.class).getFileDownloadUrl(headerMap, body);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> batchGetFileDownloadUrl(Map<String, String> headerMap, BatchGetFileDownloadUrl batchGetFileDownloadUrl) {
        try {
            RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"), GsonUtils.getGson().toJson(batchGetFileDownloadUrl));
            return getYunRetrofit().create(SytApi.class).batchGetFileDownloadUrl(headerMap, body);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    public Observable<String> getDirectUploadUrls(Map<String, String> headerMap, Long corpId,
                                                  Integer day, Integer urlTimes, String clientIp) {
        try {
            return getYunRetrofit().create(SytApi.class)
                    .getDirectUploadUrls(headerMap, corpId, day, urlTimes, clientIp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> realUploadFile(Map<String, Object> headerMap,
                                             String date, String authorization) {
        try {
            return getYunRetrofit().create(SytApi.class)
                    .realUploadFile(headerMap, date, authorization);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<String> createFile4Direct(Map<String, String> headerMap, Long corpId,
                                                Long parentId, String fileName,
                                                Long size, String md5,
                                                String objectId, String eTag,
                                                Long contentLength) {
        try {
            return getYunRetrofit().create(SytApi.class)
                    .createFile4Direct(headerMap, corpId, parentId, fileName,
                            size, md5, objectId, eTag, contentLength);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<JsonObject> createAccount(String yunAppkey, String timestamp, String format,
                                                String version, String paras, String sign) {
        //MainActivity.yunAppkey, timestamp,version,paras,
        //                        sign
        try {
            return getYunRetrofit().create(SytApi.class)
                    .createAccount(yunAppkey, timestamp, format, version, paras, sign);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public Observable<String> getQR(String userId,int height) {
        try {
            return getQrRetrofit().create(SytApi.class).getQRcode(userId,height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
